﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace WPFClient
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Navigate("Home");
        }

        private void btnNav_Click(object sender,RoutedEventArgs e)
        {
            Button btn = sender as Button;
            Navigate(btn.Tag.ToString());
        }

        private void Navigate(string path)
        {
            string uri = "WPFClient." + path;
            Type type = Type.GetType(uri);
            if(type != null)
            {
                object obj = type.Assembly.CreateInstance(uri);
                BasePage control = obj as BasePage;
                this.frmMain.Content = control;
                PropertyInfo[] infos = type.GetProperties();
                foreach(PropertyInfo info in infos)
                {
                    //将MainWindow设为Page页的ParentWin
                    if(info.Name == "ParentWindow")
                    {
                        info.SetValue(control, this, null);
                        break;
                    }
                }
            }
        }

        //公共方法
        public void CallFromChild(string name)
        {
            MessageBox.Show("Hello," + name + "!");
        }

        private bool _allowDirectNavigation = false;
        private NavigatingCancelEventArgs _navArgs = null;
        private void frmMain_Navigated(object sender, NavigationEventArgs e)
        {
            //MessageBox.Show("frmMain_Navigated");

            //if (Content != null && !_allowDirectNavigation)

            
        }

        private void FadeOutCompleted(object sender, EventArgs e)
        {
            (sender as AnimationClock).Completed -= FadeOutCompleted;

            this.IsHitTestVisible = true;

            
            _allowDirectNavigation = false;
            /* switch (_navArgs.NavigationMode)
             {
                 case NavigationMode.New:
                     if (_navArgs.Uri == null)
                     {
                         NavigationService.Navigate(_navArgs.Content);
                     }
                     else
                     {
                         NavigationService.Navigate(_navArgs.Uri);
                     }
                     break;
                 case NavigationMode.Back:
                     NavigationService.GoBack();
                     break;

                 case NavigationMode.Forward:
                     NavigationService.GoForward();
                     break;
                 case NavigationMode.Refresh:
                     NavigationService.Refresh();
                     break;
             }*/

            Dispatcher.BeginInvoke(DispatcherPriority.Loaded,
                (ThreadStart)delegate ()
                {
                    DoubleAnimation da = new DoubleAnimation(1.0d, new Duration(TimeSpan.FromMilliseconds(200)));
                    frmMain.BeginAnimation(OpacityProperty, da);

                    //DoubleAnimation widthAnimation = new DoubleAnimation()
                    //{
                    //    From = 0,
                    //    To = 320,
                    //    Duration = TimeSpan.FromSeconds(1),
                    //    EasingFunction = new BackEase()
                    //    {
                    //        Amplitude = 0.3,
                    //        EasingMode = EasingMode.EaseOut,
                    //    },
                    //};

                    //this.BeginAnimation(OpacityProperty, da);
                   // frmMain.BeginAnimation(WidthProperty, widthAnimation);
                });
        }

        private void frmMain_Navigating(object sender, NavigatingCancelEventArgs e)
        {
            if (!_allowDirectNavigation)
            {
                //e.Cancel = true;
                _allowDirectNavigation = true;
                _navArgs = e;
                this.IsHitTestVisible = false;
                DoubleAnimation da = new DoubleAnimation(0.1d, new Duration(TimeSpan.FromMilliseconds(300)));
                da.Completed += FadeOutCompleted;
                //this.BeginAnimation(OpacityProperty, da);
                frmMain.BeginAnimation(OpacityProperty, da);

                //DoubleAnimation widthAnimation = new DoubleAnimation()
                //{
                //    From = 0,
                //    To = 320,
                //    Duration = TimeSpan.FromSeconds(1),
                //    EasingFunction = new BackEase()
                //    {
                //        Amplitude = 0.3,
                //        EasingMode = EasingMode.EaseOut,
                //    },
                //};
                //frmMain.BeginAnimation(WidthProperty, widthAnimation);
            }
        }
    }
}
